<template>
  <div class="app-container">
    <div class="mainbox">
      <dt-table
        v-loading="loading"
        :table-opts="tableOpts"
        :pagination-data="{
          total:total,
          queryParams:queryParams
        }"
        :is-column-search="false"
        @pagination="pagination"
        @searchReset="handleReset"
        @searchChange="handleQuery"
      />
    </div>
    <addInterface ref="addInterface" />
  </div>
</template>

<script>
import interfaceApi from '@/framework/api/workflow/interfaceManage/index'
import addInterface from './components/addInterface.vue'

export default {
  name: 'FlowInterfaceManagement',
  components: {
    addInterface
  },
  data() {
    return {
      tableOpts: {
        tableData: [
        ],
        rowKey: 'positionId',
        toolbarBtn: [
          {
            label: '新建接口',
            permission: ['FLOWINTERFACEMANAGEMENT_ADD_BUTTON'],
            type: 'primary',
            click: (type) => {
              this.handleEdit('create', '')
            }
          }
        ],
        firstTableCol: {
          select: false,
          width: 55,
          type: 'selection'
        },
        tableCol: [
          {
            prop: 'apiName',
            label: '接口名称',
            show: true,
            search: true
          },
          {
            prop: 'appName',
            label: '所属应用',
            show: true
          },
          {
            prop: 'appCode',
            label: '所属应用',
            show: false,
            search: true,
            type: 'dict',
            selectValue: 'appCode',
            selectLabel: 'appName',
            formatter: () => {
              return this.appList
            }
          },
          {
            prop: 'rwType',
            label: '接口类型',
            show: true,
            formatter: (data, prop) => {
              return data.rwType === 1 ? '读' : '写'
            }
          },
          {
            prop: 'rwType',
            label: '接口类型',
            show: false,
            search: true,
            searchType: 'select',
            searchData: () => {
              return [
                { label: '读', value: 1 },
                { label: '写', value: 2 }
              ]
            }
          },
          {
            prop: 'apiRemark',
            label: '描述',
            show: true
          },

          {
            prop: 'updateTime',
            label: '最近修改时间',
            show: true
          },
          {
            prop: 'updateUserName',
            label: '最近修改人',
            show: true
          },
          {
            prop: 'createTime',
            label: '创建时间',
            show: true
          },
          {
            prop: 'createUserName',
            label: '创建人',
            show: true
          }

        ],
        operator: {
          width: 156,
          operatorBtn: [
            {
              text: '修改',
              size: 'small',
              click: (row, col, cellValue) => { return this.handleEdit('update', row) },
              permission: ['FLOWINTERFACEMANAGEMENT_UPDATE_BUTTON']

            },
            {
              text: '删除',
              type: 'danger',
              size: 'small',
              click: (row, col, cellValue) => { return this.removePosition(row) },
              permission: ['FLOWINTERFACEMANAGEMENT_DEL_BUTTON']

            }
          ]
        }
      },
      total: 0,
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        apiName: '',
        appCode: '',
        rwType: ''
      },
      loading: false,
      appList: []
    }
  },
  created() {
    this.getAppList()
    this.getList()
  },
  methods: {
    handleEdit(type, row) {
      this.$refs.addInterface.openDialog(type, row)
    },
    async getAppList() {
      const { data } = await interfaceApi.getStatusThirdAppList()
      this.appList = data
    },
    getList() {
      this.loading = true
      interfaceApi.getApiPage(this.queryParams).then((res) => {
        this.tableOpts.tableData = res.data.rows
        this.total = res.data.totalRows
        this.loading = false
      })
    },
    handleQuery(formModel) {
      this.queryParams = { ...this.queryParams, ...formModel }
      this.queryParams.pageNo = 1
      this.getList()
    },
    handleReset() {
      this.queryParams = {
        pageNo: 1,
        pageSize: 10,
        apiName: '',
        appCode: '',
        rwType: ''
      }
      this.handleQuery()
    },
    handleDetail(row) {
      this.detailOption.data = row
      this.$refs.dtDetailRef.dialogVisible = true
    },
    // 删除
    removePosition(row) {
      this.$dtMessageBox({ title: '提示', message: `确定要删除“${row.apiName}”吗?`, type: 'warning' }).then(() => {
        interfaceApi.deleteApi(row).then((res) => {
          this.getList()
          this.$dtMessage({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
        })
      })
    },

    pagination() {
      this.getList()
    }
  }
}
</script>
